//
//  BaseTabBarViewController.m
//  bus
//
//  Created by ygs on 15/6/6.
//  Copyright (c) 2015年 ygs. All rights reserved.
//

#import "BaseTabBarViewController.h"
#import "HomeViewController.h"
#import "ShoppingCarViewController.h"
#import "LifeViewController.h"
#import "LazyCartViewController.h"
#import "MyViewController.h"

#define PressTextColor SetColor(13,153,68,1)
#define NomalTextColor SetColor(100,100,100,1)
@interface BaseTabBarViewController ()
{
    ShoppingCarViewController *shoppingCar;
    LazyCartViewController *lazyVC;
    NSInteger oldIndex;
}
@end

@implementation BaseTabBarViewController
-(void)viewWillAppear:(BOOL)animated
{
    [self.selectedViewController beginAppearanceTransition: YES animated: animated];
}

-(void) viewDidAppear:(BOOL)animated
{
    [self.selectedViewController endAppearanceTransition];
}

-(void) viewWillDisappear:(BOOL)animated
{
    [self.selectedViewController beginAppearanceTransition: NO animated: animated];
}

-(void) viewDidDisappear:(BOOL)animated
{
    [self.selectedViewController endAppearanceTransition];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // tabBar图片
    imgNomalAry =[[NSArray alloc] initWithObjects:@"n1.png",@"n2.png",@"n4.png",@"n5.png",nil];
    // tabBar选择后的图片数组
    imgPressAry =[[NSArray alloc] initWithObjects:@"s1.png",@"s2.png",@"s4.png",@"s5.png",nil];
//    titleArr = @[@"首页",@"懒猫超市",@"生活圈",@"懒猫圈",@"我的"];
    
    [self initViewController];
    [self initTabBar];
    self.tabBar.hidden = YES;
    
    [AppData share].baseTabVC = self;
}

#pragma mark - tabBarView
-(void)initTabBar
{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight-50, ScreenWidth, 50)];
    view.backgroundColor=[UIColor whiteColor];
    self.tabView=view;
    self.title=@"";
    [self.view addSubview:_tabView];
    
    
    int btnSize=ScreenWidth/imgNomalAry.count;
    for (int i=0; i<imgNomalAry.count; i++)
    {
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame=CGRectMake(i*btnSize+(btnSize - 40)/2, (50-36)/2, 40, 36);
        btn.tag=i+20;
        if (i == 0) {
            oldIndex = 0;
            [btn setBackgroundImage:[UIImage imageNamed:[imgPressAry objectAtIndex:i]] forState:UIControlStateNormal];
        }else{
            [btn setBackgroundImage:[UIImage imageNamed:[imgNomalAry objectAtIndex:i]] forState:UIControlStateNormal];
        }
        btn.backgroundColor = [UIColor whiteColor];
        btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        btn.adjustsImageWhenHighlighted = NO;
        [btn setHitTestEdgeInsets:UIEdgeInsetsMake(0, -(btnSize - 40)/2, 0, -(btnSize - 40)/2)];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//        [btn setShowsTouchWhenHighlighted:YES];
        [_tabView addSubview:btn];
        
        
        // 购物车小红点
        if (i == 2) {
            //购物车 数量
            _countLable = [[UILabel alloc] initWithFrame:CGRectMake(btn.right-20, 0, 20, 20)];
            _countLable.backgroundColor = [UIColor redColor];
            _countLable.font = FontSet(12);
            _countLable.layer.cornerRadius = 10;
            _countLable.hidden = YES;
            _countLable.textAlignment = NSTextAlignmentCenter;
            _countLable.text = @"0";
            _countLable.textColor = [UIColor whiteColor];
            _countLable.layer.masksToBounds = YES;
            [_tabView addSubview:_countLable];
            
         // 我的页面小红点
        }else if (i == 3) {
            //个人中心小红点
            _redLable = [[UILabel alloc] initWithFrame:CGRectMake(btn.right-10, 5, 6, 6)];
            _redLable.backgroundColor = [UIColor redColor];
            _redLable.layer.cornerRadius = 3;
            _redLable.layer.masksToBounds = YES;
            _redLable.hidden = YES;
            [_tabView addSubview:_redLable];
        }
    }
    
    UILabel *lineLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
    lineLable.backgroundColor = SetColor(0, 0, 0, 0.1);
    [view addSubview:lineLable];
}

- (void)initViewController{
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    lazyVC = [[LazyCartViewController alloc] init];
    shoppingCar = [[ShoppingCarViewController alloc] init];
    MyViewController *myVC = [[MyViewController alloc] init];
    
    NSArray *controllerArr = @[homeVC,lazyVC,shoppingCar,myVC];
    NSMutableArray *mArr = [[NSMutableArray alloc] init];
    for (int i =0; i<controllerArr.count; i++) {
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[controllerArr objectAtIndex:i]];
        nav.delegate = self;
        nav.navigationBarHidden = YES;
        [mArr addObject:nav];
    }
    
    self.viewControllers = mArr;

}

#pragma mark - tabBar点击
- (void)btnClick:(UIButton *)btn{
    [self changeBottomBtnWithIndex:btn.tag-20];
}
- (void)changeBottomBtnWithIndex:(NSInteger)index{
    for (int i=0; i<imgPressAry.count; i++) {
        UIButton *tabBtn = (UIButton *)[_tabView viewWithTag:20+i];
        [tabBtn setBackgroundImage:[UIImage imageNamed:[imgNomalAry objectAtIndex:i]] forState:UIControlStateNormal];
        
        UILabel *lable = (UILabel *)[_tabView viewWithTag:30+i];
        lable.textColor = NomalTextColor;
    }
    UIButton *btn = (UIButton*)[_tabView viewWithTag:20+index];
    [btn setBackgroundImage:[UIImage imageNamed:[imgPressAry objectAtIndex:index]] forState:UIControlStateNormal];
    
    UILabel *lable = (UILabel *)[_tabView viewWithTag:30+index];
    lable.textColor = PressTextColor;
    
    self.selectedIndex = index;
//    
//    if (index != oldIndex) {
//        if (index == 2) {
//            //点击购物车 判断是否登陆
//            [shoppingCar refresh];
//        }else if (index == 8) { // 点击第几个tabBar进行刷新
//            [lazyVC refresh]; // 刷新
//        }
//    }
//    oldIndex = index;
    
}
#pragma mark -navigation delegate
//- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
//    NSInteger coumt=navigationController.viewControllers.count;
//    if (coumt>=2)
//    {
//        
//        [UIView animateWithDuration:.05 animations:^{
//            _tabView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 50);
//        } completion:^(BOOL finished) {
//            _tabView.hidden=YES;
//        }];
//    }
//    else {
//        _tabView.hidden=NO;
//        
//        [UIView animateWithDuration:.05 animations:^{
//            _tabView.frame = CGRectMake(0, ScreenHeight-50, ScreenWidth, 50);
//        }];
//    }
//}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    NSInteger coumt=navigationController.viewControllers.count;
    if (coumt>=2)
    {
        
        [UIView animateWithDuration:.15 animations:^{
            _tabView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 50);
        } completion:^(BOOL finished) {
            _tabView.hidden=YES;
        }];
    }else {
        _tabView.hidden=NO;
        
        [UIView animateWithDuration:.15 animations:^{
            _tabView.frame = CGRectMake(0, ScreenHeight-50, ScreenWidth, 50);
        }];
    }
}
#pragma mark -  
- (void)doReduce:(BOOL)doReduce {
//    NSInteger currentCount = [_countLable.text integerValue];
//    if (doReduce) {
//        currentCount -= 1;
//    }else {
//        currentCount +=1;
//    }
//    
//    if (currentCount <= 0) {
//        currentCount = 0;
//        _countLable.hidden = YES;
//    }else {
//        _countLable.hidden = NO;
//    }
//    
//    _countLable.text = [NSString stringWithFormat:@"%ld",(long)currentCount];
}

#pragma mark - 购物车数量
- (void)theShoppingCartCount:(NSString *)count {
    _countLable.text = [NSString stringWithFormat:@"%@",count];
    if ([count intValue] <= 0) {
        _countLable.hidden = YES;
    }else {
        _countLable.hidden = NO;
    }
}
#pragma mark - 跳转到登录界面
- (void)goLoginView {
    LoginViewController *login = [[LoginViewController alloc] init];
    [self presentViewController:login animated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
